﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using AncientLand.Service.Entity;
using AncientLand.Service.IBiz;

namespace AncientLand.Controllers.Admin
{
   public class ActivityAdminController: Controller
    {
       private readonly IActivitiesBiz _activitiesService;

       public ActivityAdminController(IActivitiesBiz activitiesService)
        {
            this._activitiesService = activitiesService;
        }


       [Authorize]
       public ActionResult List(int? page = 1,int? pageSize=10)
       {
           ViewBag.Model = "Activities";

           var size = Convert.ToInt16(System.Configuration.ConfigurationSettings.AppSettings["AdminpageSize"]); 

           var totalRecord = 0;
           var news = _activitiesService.GetItemsByPages(out totalRecord, pageIndex: page.GetValueOrDefault(), pageSize: size, orderFiled: "");

           var item = new PageItem()
           {
               CurrentPage = page.GetValueOrDefault(),
               TotalRecords = totalRecord,
               TotalPages = (totalRecord % size == 0) ? totalRecord / size : totalRecord / size + 1,
               ControllerName = "ActivityAdmin",
               ActionName = "List"
           };

           var viewModels = new Pager<Activities>() { DataSource = news, Item = item };

           return View(viewModels);
       }

       [Authorize]
       public ActionResult Edit(int? id=1)
       {
           ViewBag.Model = "Activities";

           var newsItem = _activitiesService.GetById(id.GetValueOrDefault());
           return View(newsItem);
       }

       [Authorize]
       [HttpPost]
       [ValidateInput(false)]
       public RedirectToRouteResult Edit(Activities activities)
       {
           if (ModelState.IsValid)
           {
               activities.CreatedTime = DateTime.Now;
               _activitiesService.Update(activities);
           }
           return RedirectToAction("List");
       }

       [Authorize]
       public ActionResult Add()
       {
           ViewBag.Model = "Activities";
           return View();
       }

       [Authorize]
       [HttpPost]
       [ValidateInput(false)]
       public RedirectToRouteResult Add(Activities activities)
       {
           if (ModelState.IsValid)
           {
               activities.CreatedTime = DateTime.Now;
               activities.ReadTimes = 1;
               _activitiesService.Create(activities);
           }
           return RedirectToAction("List");
       }

       [Authorize]
       public ActionResult Delete(int id)
       {
           _activitiesService.Delete(id);
           return RedirectToAction("List");
       }

    }

   
}
